package cn.sharetraffic.mi.etongdai;

import cn.sharetraffic.mi.FindListener;
import cn.sharetraffic.mi.PushApiManager;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.List;

/**
 * 功能：六个月债权
 * 作者：蒋睿立
 * 时间：2017/7/25
 */
public class SixDebt extends BaseETDCustomer{
    private long lastPushTime;//上次发送通知时间
    private int count;//次数
    private final HashMap<String, String> httpPara = new HashMap<String, String>();
    private Logger log = Logger.getLogger(getClass());

    public SixDebt(){
        httpPara.put("type","");
        httpPara.put("scope","10-15");
        httpPara.put("repay","6");
        httpPara.put("repayType","");
        httpPara.put("orderArgs","1");
        httpPara.put("ascDesc","desc");
        httpPara.put("pageId","1");
    }


    @Override
    public void shopping() {

        List<DebtEntity> list = packageEntity(httpPara);
        if (list == null){
            return;
        }
        log.info("遍历六个月等额本息");
        for (DebtEntity entity:list) {
            result(entity);
        }
    }


    /**
     * 判断返回数据并处理结果
     * @param entity
     */
    private void result(DebtEntity entity) {
        try{
            double p = entity.getProgress();
            double r = entity.getRate();
            if (p < 90 && r > 13) {
                log.info("六个月债权有满足条件！");
                if (nowTime - lastPushTime > 3L * 60 * 1000 && count < 2) {
                    // 离上次通知大于3分钟就短信通知，最多通知2次客户端
                    lastPushTime = nowTime;
                    count++;
                    PushApiManager.getInstance().sendBySms("收益率："+entity.getRate()+"%,"+
                            "当前进度："+entity.getProgress());
                    log.info("推送满足条件的结果！"+entity.toString());
                } else if(nowTime - lastPushTime > 60L * 60 *1000){
                    //一小时后重置，可以继续推送
                    count = 0;
                }
            }
        }catch (Exception e){
            log.error(e.getMessage(), e);
        }
    }
}
